﻿using Common;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using ZWZTools.BaseUI;
using ZWZTools.Common;
using ZWZTools.Model;

namespace ZWZTools.Forms
{
    public partial class VideoParsing : BaseForm
    {
        delegate void AddListViewItem(AnalysisUrl item);
        delegate void SetDefaultSelect(AnalysisUrl item);
        AddListViewItem addListViewItem;
        SetDefaultSelect setDefaultSelect;
        AnalysisUrlData ReadData;
        AnalysisUrl SelectedItem;
        public VideoParsing()
        {
            InitializeComponent();
            InitCom();
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitCom()
        {
            lv_url.MultiSelect = false;
            lv_url.LabelEdit = false;
            lv_url.View = View.Details;
            lv_url.FullRowSelect = true;
            ColumnHeader columnHeader1 = new ColumnHeader();
            columnHeader1.Text = "解析资源";
            lv_url.Columns.Add("解析资源", 100);
            //lv_url.Columns.Add("url", 140);
            lv_url.Columns.Add("备注", 150);
        }

        #region 窗口事件

        /// <summary>
        /// 窗口加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            ReadUrlList();
        }

        /// <summary>
        /// 窗口关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VideoParsing_FormClosed(object sender, FormClosedEventArgs e)
        {
            AppSettings.Instance.Save();
            FormHelper.ShowMain(this);
        }

        #endregion

        /// <summary>
        /// 读取线路库数据
        /// </summary>
        private void ReadUrlList()
        {
            string jsonfile = "Config/URL.json";//JSON文件路径
            using (StreamReader sr = File.OpenText(jsonfile))
            {
                string json = sr.ReadToEnd();
                addListViewItem = new AddListViewItem(AddItem);
                setDefaultSelect = new SetDefaultSelect(SetDefault);
                ReadData = JsonHelper.FromJson<AnalysisUrlData>(json);
                Thread thread = new Thread(new ParameterizedThreadStart(InitData));
                thread.Start(ReadData);
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="_data"></param>
        private void InitData(object _data)
        {
            AnalysisUrlData data = _data as AnalysisUrlData;
            foreach (AnalysisUrl item in data.data)
            {
                this.BeginInvoke(addListViewItem, item);
            }
            this.BeginInvoke(setDefaultSelect, AppSettings.Instance.LastUrl);
        }

        /// <summary>
        /// 添加项
        /// </summary>
        /// <param name="item"></param>
        private void AddItem(AnalysisUrl item)
        {
            ListViewItem lvi = new ListViewItem(string.IsNullOrEmpty(item.label) ? "线路" + lv_url.Items.Count : item.label);//第一列
            //lvi.SubItems.Add(item.url);//第二列
            lvi.SubItems.Add(item.remarks);//第三列
            lv_url.Items.Add(lvi);//加入一行到ListView
        }

        /// <summary>
        /// 设置默认选中
        /// </summary>
        /// <param name="item"></param>
        private void SetDefault(AnalysisUrl item)
        {
            if (item == null)
            {
                lv_url.Items[0].Selected = true;
            }
            else
            {
                int index = ReadData.data.FindIndex(t => t.label == item.label && t.remarks == item.remarks && t.url == item.url);
                if (index == -1)
                {
                    lv_url.Items[0].Selected = true;
                }
                else
                {
                    lv_url.Items[index].Selected = true;
                }
            }
        }

        /// <summary>
        /// 线路选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lv_url_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                SelectedItem = ReadData.data[e.ItemIndex];
                label3.Text = SelectedItem.label;
                label4.Text = SelectedItem.url;
                label5.Text = SelectedItem.remarks;
            }
        }

        /// <summary>
        /// 开始解析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_start_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Path.Combine(SelectedItem.url, tb_url.Text.ToString()));  //打开系统默认浏览器
            AppSettings.Instance.LastUrl = SelectedItem;
        }

        /// <summary>
        /// 刷新线路库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lb_refenceUrlList_Click(object sender, EventArgs e)
        {
            var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var fileName = dir + "\\Config\\URL.json";
            if (!Directory.Exists(dir + "\\Config"))
            {
                Directory.CreateDirectory(dir + "\\Config");
            }
            var jsonStr = JsonHelper.ToJson(ReadData);
            File.WriteAllText(fileName, jsonStr);
        }

        /// <summary>
        /// 右键菜单 - 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_delete_Click(object sender, EventArgs e)
        {
            int index = ReadData.data.FindIndex(t => t.label == SelectedItem.label && t.remarks == SelectedItem.remarks && t.url == SelectedItem.url);
            if (index == -1)
            {
                MessageBox.Show("出错啦");
            }
            else
            {
                lv_url.Items.RemoveAt(index);
                ReadData.data.RemoveAt(index);
            }
        }

        private void lv_url_MouseClick(object sender, MouseEventArgs e)
        {
            ListView listView = (ListView)sender;
            ListViewItem item = listView.GetItemAt(e.X, e.Y);
            if (item != null && e.Button == MouseButtons.Right)
            {
                this.cms.Show(listView, e.X, e.Y);
            }
        }
    }
}
